<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

if(isset($_SESSION['u_nic'])&&isset($_GET['id'])) {
	$tpl->assign(array(
		'iconesleft' => 2,
		'prod' => '0',
		'prod_id' => intval($_GET['id'])
	));
	include('php/lib/panel_inc.php');
	$heberg_current = intval($_GET['id']);
	$infos_heberg = mysql_query("SELECT comptes_heberg.id,
										comptes_heberg.u_nic,
										comptes_heberg.uid_linux,
										comptes_heberg.user_linux,
										comptes_heberg.serveur_apache,
										comptes_heberg.offre,
										comptes_heberg.etat,
										comptes_heberg.ftp AS c_ftp,
										offre_heberg.user_ftp AS o_ftp,
										offre_surmesure.user_ftp as o_ftp_
										FROM comptes_heberg 
										LEFT JOIN offre_heberg
										ON comptes_heberg.offre = offre_heberg.id_offre
										LEFT JOIN offre_surmesure
										ON comptes_heberg.id = offre_surmesure.id_heberg
										WHERE comptes_heberg.id='".$heberg_current."' AND comptes_heberg.u_nic='".$_SESSION['u_nic']."'")or die(mysql_error());
	if(mysql_num_rows($infos_heberg) == 1) {
		$infos_heberg = mysql_fetch_array($infos_heberg);
		if($infos_heberg['o_ftp'] == "") {
			$infos_heberg['o_ftp'] = $infos_heberg['o_ftp_'];
		}
		$tpl->assign(array('etat' => $infos_heberg['etat']));
		if($infos_heberg['etat'] == 1) {
			if($_GET['action'] == 'add') {
				if($infos_heberg['c_ftp'] < $infos_heberg['o_ftp']) {
				
					if(isset($_POST['login_ftp'])) {
						if($_POST['password1'] == $_POST['password2']) {
							$login = $infos_heberg['user_linux'].'-'.preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['login_ftp']);
							$exist = mysql_fetch_array(query("SELECT COUNT(*) AS exist FROM comptes_ftp WHERE userid='".$login."'"));
							if($exist['exist'] == 0) {
								$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
								if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
								$pass = mysql_real_escape_string($_POST['password1']);
								query("INSERT INTO comptes_ftp(id_heberg, userid, uid, gid, shell, homedir, serveur, password) VALUES('".$infos_heberg['id']."', '".$login."', '".$infos_heberg['uid_linux']."', '".$infos_heberg['uid_linux']."', '/bin/bash', '/home/".$infos_heberg['user_linux'].$dossier."', '".$infos_heberg['serveur_apache']."', ENCRYPT('".$pass."'))");
								query("UPDATE comptes_heberg SET ftp = ftp+1 WHERE id='".$infos_heberg['id']."'");
								header("Location:hebergement-ftp.html?id=".intval($_GET['id']));
							}
							else {
								$parse = 1;
								$tpl->assign(array(
									'user_linux' => $infos_heberg['user_linux'],
									'erreur' => 2
								));
								$title = "Ajouter un compte FTP";
								$tplinc = "panel-hebergement-ftp-add";
							}
						}
						else {
							$parse = 1;
							$tpl->assign(array(
								'user_linux' => $infos_heberg['user_linux'],
								'erreur' => 1
							));
							$title = "Ajouter un compte FTP";
							$tplinc = "panel-hebergement-ftp-add";
						}
					}
					else {
							$title = "Ajouter un compte FTP";
							$tplinc = "panel-hebergement-ftp-add";
							$parse = 1;
							$tpl->assign(array(
								'user_linux' => $infos_heberg['user_linux']
							));
					}
				}
				else {
					header("Location:hebergement-ftp.html?id=".intval($_GET['id']));
				}
			}
			elseif($_GET['action'] == 'del') {
				if(isset($_GET['compte_ftp'])) {
					$compte_ftp = intval($_GET['compte_ftp']);
					query("DELETE FROM comptes_ftp WHERE id_heberg='".$heberg_current."' AND id='".$compte_ftp."'");
					query("UPDATE comptes_heberg SET ftp = ftp-1 WHERE id='".$infos_heberg['id']."'");
				}
				header("Location:hebergement-ftp.html?id=".intval($_GET['id']));
			}
			elseif($_GET['action'] == 'edit') {
				$compte_ftp = mysql_fetch_array(query("SELECT id,userid,homedir,uid,id_heberg FROM comptes_ftp WHERE id='".intval($_GET['compte_ftp'])."' AND id_heberg='".$heberg_current."'"));
				if(isset($_POST['dossier'])) {
					if($_POST['password1'] == $_POST['password2']) {
						$dossier = preg_replace('#([^a-zA-Z0-9\-\_/])+#i', '', $_POST['dossier']);
						if(!substr($dossier,0,1) == "/") { $dossier = "/".$dossier; }
						$dossier = '/home/'.$infos_heberg['user_linux'].$dossier;
						$pass = mysql_real_escape_string($_POST['password1']);
						if($pass != "") {
							query("UPDATE comptes_ftp SET homedir='".$dossier."', password=ENCRYPT('".$pass."') WHERE id='".intval($_GET['compte_ftp'])."' AND id_heberg='".$heberg_current."'");
						}
						else {
							query("UPDATE comptes_ftp SET homedir='".$dossier."' WHERE id='".intval($_GET['compte_ftp'])."' AND id_heberg='".$heberg_current."'");
						}
						header("Location:hebergement-ftp.html?id=".intval($_GET['id']));
					}
					else {
						$parse = 1;
						$tpl->assign(array(
							'user_linux' => $infos_heberg['user_linux'],
							'homedir' => $compte_ftp['homedir'],
							'login_ftp' => $compte_ftp['userid'],
							'erreur' => 1
						));
						$title = "Modifier un compte FTP";
						$tplinc = "panel-hebergement-ftp-mod";
					
					}
				}
				else {
						$parse = 1;
						$homedir = explode('/home/'.$infos_heberg['user_linux'], $compte_ftp['homedir']);
						$tpl->assign(array(
							'user_linux' => $infos_heberg['user_linux'],
							'homedir' => $homedir[1],
							'login_ftp' => $compte_ftp['userid'],
							'erreur' => 1
						));
						$title = "Modifier un compte FTP";
						$tplinc = "panel-hebergement-ftp-mod";
				
				}		
			}
			else{ 
				$title = "Gestion des comptes FTP";
				$tplinc = "panel-hebergement-ftp";
				$parse = 1;	
				if($infos_heberg['o_ftp'] == "") {
					$infos_heberg['o_ftp'] = "Illimités";
				}
				$tpl->assign(array(
					"serveur_ftp_i" => $server[$infos_heberg['serveur_apache']]['ip'],
					"serveur_ftp_n" => $server[$infos_heberg['serveur_apache']]['ftp'],
					"o_ftp" => $infos_heberg['o_ftp'],
					"c_ftp" => $infos_heberg['c_ftp'],
				));
				$comptes_ftp = query("SELECT id, userid, id_heberg, uid, homedir FROM comptes_ftp WHERE id_heberg='".$infos_heberg['id']."'");
				While($cftp = mysql_fetch_array($comptes_ftp)) {
					$homedir = explode('/home/'.$infos_heberg['user_linux'], $cftp['homedir']);
					$tpl->assign_array('comptes_ftp',array(
						'userid' => $cftp['userid'],
						'homedir' => $homedir[1],
						'id' => $cftp['id'],
					));	
				}
			}
		}
		else { header("Location:/panel/hebergement.html?id=".intval($_GET['id'])); }
	}
}
else { header("Location:/site/connexion.html"); }

?>